﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace net第四次作业
{
    public partial class Form1 : Form
    {
        private string path;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // 创建代表C盘根目录的顶级节点
                TreeNode rootNode = new TreeNode("C:");
                rootNode.Tag = @"C:\";
                treeView1.Nodes.Add(rootNode);

                // 加载顶层目录的子目录
                LoadChildren(rootNode);

                // 初始化ListView
                listView1.View = View.Details;
                listView1.Columns.Add("FileName", -2, HorizontalAlignment.Left);
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //这里用了懒加载实现多级目录，不需要一下子将所有的文件都递归一遍
        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            // 检查是否已经加载了子节点
            if (e.Node.Nodes.Count == 0 && e.Node.Tag != null)
            {
                // 加载子目录
                LoadChildren(e.Node);
            }
            else
            {   //实现多级目录
                foreach (var node in e.Node.Nodes)
                {

                    LoadChildren((TreeNode)node);
                }
            }
        }

        private void LoadChildren(TreeNode node)
        {
            string path = node.Tag.ToString();
            //清除现有子节点
            node.Nodes.Clear();
            try
            {   //这里有个错误，无法访问某些没有权限的文件夹或者是文件，需要一些别的方式处理
                foreach (var directory in Directory.GetDirectories(path))
                {
                    TreeNode childNode = new TreeNode(new DirectoryInfo(directory).Name);
                    childNode.Tag = directory;
                    node.Nodes.Add(childNode);
                }

                foreach (var file in Directory.GetFiles(path))
                {
                    TreeNode fileNode = new TreeNode(Path.GetFileName(file));
                    fileNode.Tag = file;
                    node.Nodes.Add(fileNode);

                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {   
            //树形视图中被选中的文件夹路径
            string selectedPath = e.Node.Tag.ToString();
            LoadDirectoryContents(selectedPath);
        }

        //加载选择的文件夹
        //右侧文件夹里文件的显示
        private void LoadDirectoryContents(string path)
        {
            this.path = path;
            try
            {
                listView1.Items.Clear();
                //搜索文件，指单个文件
                foreach (string file in Directory.GetFiles(path))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    ListViewItem item = new ListViewItem(fileInfo.Name);
                    item.SubItems.Add(fileInfo.Length.ToString());
                    listView1.Items.Add(item);
                }
                //搜索的是子目录，文件夹
                foreach (string directory in Directory.GetDirectories(path))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(directory);
                    ListViewItem item = new ListViewItem(dirInfo.Name);
                    item.SubItems.Add("-");
                    listView1.Items.Add(item);
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    string selectedItem = listView1.SelectedItems[0].Text;
                    string fullPath = Path.Combine(treeView1.SelectedNode.Tag.ToString(), selectedItem);

                    //可双击点开的文件
                    if (fullPath.EndsWith(".exe"))
                    {
                        Process.Start(fullPath);
                    }
                    else if (fullPath.EndsWith(".txt"))
                    {
                        Process.Start("notepad.exe", fullPath);
                    }
                    else
                    {
                        MessageBox.Show("only support .exe or .txt");
                            }
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            // 刷新当前选定的节点
            if (treeView1.SelectedNode != null)
            {
                LoadChildren(treeView1.SelectedNode);
            }
        }
        private void ExpandAllButton_Click(object sender, EventArgs e)
        {
            // 展开所有节点
            //慎重点击 需要很长时间哈哈
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("请慎重，你真的要执行吗？", "取消", messButton);
            if(dr == DialogResult.OK)
            {
                MessageBox.Show("糟糕");
                treeView1.ExpandAll();
            }
            else
            {
                MessageBox.Show("干得漂亮！");
            }

        }

        private void CollapseAllButton_Click(object sender, EventArgs e)
        {
            // 折叠所有节点
            treeView1.CollapseAll();
        }


        //仅展示目录文件
        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            try
            {   listView1.Items.Clear();
                //搜索的是子目录，文件夹
                foreach (string directory in Directory.GetDirectories(path))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(directory);
                    ListViewItem item = new ListViewItem(dirInfo.Name);
                    item.SubItems.Add("-");
                    listView1.Items.Add(item);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
